/**
 * 百度
 * https://lbsyun.baidu.com/index.php?title=uri/api/android
 * https://lbsyun.baidu.com/index.php?title=uri/api/ios
 */

import typeOfUtils from "./type-of-utils";
// @ts-ignore
import wx from 'weixin-js-sdk'
import {MapType} from "./CallMapFunction";
import {isWeiXin} from "./index";


export interface AddressInfo {
  /**
   * 经度
   */
  slng: string | number;
  /**
   * 纬度
   */
  slat: string | number;
  /**
   * 地点名称
   */
  slocation: string;
  /**
   * 城市名或者县名
   */
  scity?: string;
}

/**
 * h5调用第三方app
 */
export class AppCall {
  /**
   *
   * @param from 起点地址信息 没有就是当前点
   * @param to 终点地址信息
   * @param mapType
   */
  static callMap(from: AddressInfo, to: AddressInfo, mapType: MapType = 'baidu') {
    if (isWeiXin()) {
      wx.ready(function () {
        wx.checkJsApi({
          jsApiList: ["openLocation"],
          success: function (res) {
            wx.openLocation({
              latitude: to.slat, // 纬度，浮点数，范围为90 ~ -90
              longitude: to.slng, // 经度，浮点数，范围为180 ~ -180。
              name: to.slocation, // 位置名
              scale: 15, // 地图缩放级别,整形值,范围从1~28。默认为最大
              success(res) {
                console.log('成功了吗', res)
              },
              complete: (res) => {
                console.log(res)
              },
              fail: (e) => {
                console.log(e)
              }
            });
          }
        });
      });
    } else {
      const u = navigator.userAgent;
      const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端

      const andriodBaidu = () => {
        let f = ''
        if (typeOfUtils.isNotEmpty(from)) {
          f = `origin=name:${from.slocation}|latlng:${from.slat},${from.slng}&`
          if (from.scity && to.scity) {
            f += `origin_region=${from.scity}&destination_region=${to.scity}&`
          }
        }
        return `bdapp://map/direction?${f}destination=name:${to.slocation}|latlng:${to.slat},${to.slng}&coord_type=bd09ll&mode=driving&src=andr.baidu.openAPIdemo`
      }

      const iOSBaidu = () => {
        let f = ''
        if (typeOfUtils.isNotEmpty(from)) {
          f = `origin=name:${from.slocation}|latlng:${from.slat},${from.slng}&`
          if (from.scity && to.scity) {
            f += `origin_region=${from.scity}&destination_region=${to.scity}&`
          }
        }
        return `baidumap://map/direction?${f}destination=name:${to.slocation}|latlng:${to.slat},${to.slng}&coord_type=bd09ll&mode=driving&src=ios.baidu.openAPIdemo`
      }

      const andriodGaode = () => {
        let f = ''
        if (typeOfUtils.isNotEmpty(from)) {
          f = `&sname=${from.slocation}&slat=${from.slat}&slon=${from.slng}&`
        }
        return `amapuri://route/plan/?sourceApplication=mhc${f}&dlat=${to.slat}&dlon=${to.slng}&dname=${to.slocation}&dev=0&t=0`
      }

      const iOSGaode = () => {
        let f = ''
        if (typeOfUtils.isNotEmpty(from)) {
          f = `&sname=${from.slocation}&slat=${from.slat}&slon=${from.slng}&`
        }
        return `iosamap://path?sourceApplication=mhc${f}&dlat=${to.slat}&dlon=${to.slng}&dname=${to.slocation}&dev=0&t=0`
      }

      if (mapType == "baidu") {
        if (isAndroid) {
          window.location.href = andriodBaidu()
        } else {
          window.location.href = iOSBaidu()
        }
      } else if (mapType == "gaode") {
        if (isAndroid) {
          window.location.href = andriodGaode()
        } else {
          window.location.href = iOSGaode()
        }
      }
    }
  }
}
